function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

(function ($) {

    //扩展方法获取url参数
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    //得到url参数
    var urltype = $.getUrlParam('reurl');

})(jQuery);

function changeInfo()
{
    

    $("#place").removeAttr("disabled");
    $("#postCode").removeAttr("disabled");
    

    $("#phone").removeAttr("disabled");
    $("#mail").removeAttr("disabled");

    

    $("#infoActive").empty();

    const save = '<button class="btn btn-small btn-primary" id="saveChange" onclick= "saveChange()">保存</button>';
    const cancel = '<button class="btn btn-small btn-warning" id="cancelChange" onclick= "cancelChange()">取消</button>';

    const $mode1 = $(save);
    const $mode2 = $(cancel);

    $("#infoActive").append($mode1);
    $("#infoActive").append($mode2);
}

function saveChange()
{
    const map ={
        "place":$("#place").val(),
        "postCode":$("#postCode").val(),
        "phone":$("#phone").val(),
        "mail":$("#mail").val(),
        "hash":$("#hash").val()
    }

    console.log(map);

    const url = getUrl();

    $.ajax({
        url:url+"studentInfo/changeInfo",
        data:JSON.stringify(map),
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(id) {
            console.log(id);
            location.href="studentInfo.html";
        },
        error:function(id){
            console.log("----------");
            console.log(id);
        }
    })
}

function cancelChange()
{
    
    $("#place").attr('disabled',"");
    $("#postCode").attr('disabled',"");
    $("#phone").attr('disabled',"");
    $("#mail").attr('disabled',"");

    $("#infoActive").empty();

    const change = '<button class="btn btn-small btn-inverse" id="changeInfo" onclick= "changeInfo()">修改</button>';

    const $mode = $(change);

    $("#infoActive").append($mode);
}

$("#test").click(function () {
    console.log("啊啊啊啊");
    const url = getUrl();
    var source = new EventSource(url + "studentInfo/TT1/"+1);

    source.addEventListener('message', function (e) {
        console.log(e.data);
    }, false);

    source.addEventListener('error', function (e) {
        console.log("异常：" + e.data)
    });
});

$("#test1").click(function () {
    console.log("呃呃呃呃");
    const url = getUrl();
    var source = new EventSource(url + "studentInfo/TT2/"+1);

    $.ajax({
        url:url+"studentInfo/TT2/"+1,
        data:{},
        type: "post",
        contentType: "application/json; charset=utf-8",
        success: function(id) {
           
        },
        error:function(id){
           
        }
    })
});

$("#selectPeer").click(function () {
    const url = getUrl();
    const peer = $("#peerSelect").val();
    console.log(peer);

    var pagenum =Number($("#page").val());
    const userId = new Date().getTime();

    if(peer=="当前节点"){

        $.ajax({
            url:url+"studentInfo/getDataByPage",
            data:{
                "page":pagenum
            },
            type: "get",
            contentType: "application/json",
            success: function(map) {
                console.log(map);
    
                const data = map.data;
    
                if(map=="unLogin"){
                    location.href="login.html";
                }else{
    
                    $("#maxPage").val(map.maxPage);
    
                    $("#number").val(data.number);
                    $("#name").val(data.name);
                    $("#sex").val(data.sex);
                    $("#idNumber").val(data.idNumber);
                    $("#birth").val(data.birth);
                    $("#nation").val(data.nation);
                    $("#_class").val(data._class);
                    $("#level").val(data.level);
                    $("#college").val(data.college);
                    $("#place").val(data.place);
                    $("#postCode").val(data.postCode);
                    $("#status").val(data.status);
                    $("#phone").val(data.phone);
                    $("#mail").val(data.mail);
                    $("#studentType").val(data.studentType);
                    $("#studentProvince").val(data.studentProvince);
                    $("#middleSchool").val(data.middleSchool);
                    $("#date").val(data.date);
                    $("#major").val(data.major);
    
                    //$("#date").attr('placeholder',data.date);
    
                    $("#grade_body").empty();
    
                    $.each(data.grades, function (i, grade) {
                        console.log(grade);
    
                        const mode = 
                        '<div class="row-fluid">'+
                            '<div class="span1">'+
                                '<div>'+
                                    '<p id="id"></p>'+
                                '</div>'+
                            '</div>'+
                            '<div class="span2">'+
                                '<div>'+
                                    '<input class="input-medium disabled" id="name" type="text" placeholder="" disabled="">'+
                                '</div>'+
                            '</div>'+
                            '<div class="span1">'+
                                '<div>'+
                                    '<input class="input-mini disabled" id="credit" type="text" placeholder="" disabled="">'+
                                '</div>'+
                            '</div>'+
                            '<div class="span2">'+
                                '<div>'+
                                    '<input class="input-medium disabled" id="type" type="text" placeholder="" disabled="">'+
                                '</div>'+
                            '</div>'+
                            '<div class="span2">'+
                                '<div>'+
                                    '<input class="input-medium disabled" id="semester" type="text" placeholder="" disabled="">'+
                                '</div>'+
                            '</div>'+
                            '<div class="span2">'+
                                '<div>'+
                                    '<input class="input-medium disabled" id="score" type="text" placeholder="" disabled="">'+
                                '</div>'+
                            '</div>'+
                            '<div class="span2" id="grade_active">'+
                            '</div>'+
                        '</div>';
                        
                        const $obj = $(mode);
                        $obj.find("#id").text(grade.id);
                        $obj.find("#name").val(grade.name);
                        $obj.find("#credit").val(grade.credit);
                        $obj.find("#type").val(grade.type);
                        $obj.find("#semester").val(grade.semester);
                        $obj.find("#score").val(grade.score);
    
                        $("#grade_body").append($obj);
    
                    })
    
                }
    
                
                
            },
            error:function(){
                console.log("----------")
            }
        })

    }else{

        var source = new EventSource(url + "studentInfo/getPeerData/"+userId);

        source.addEventListener('message', function (e) {
            console.log("在听了");
            console.log(e.data);
            map=JSON.parse(e.data);
            const d = map.data;

            console.log(map);

            if(map=="unLogin"){
                location.href="login.html";
            }else{

                $("#hash").val(map.hash);

                $("#number").val(d.number);
                $("#name").val(d.name);
                $("#sex").val(d.sex);
                $("#idNumber").val(d.idNumber);
                $("#birth").val(d.birth);
                $("#nation").val(d.nation);
                $("#_class").val(d._class);
                $("#level").val(d.level);
                $("#college").val(d.college);
                $("#place").val(d.place);
                $("#postCode").val(d.postCode);
                $("#status").val(d.status);
                $("#phone").val(d.phone);
                $("#mail").val(d.mail);
                $("#studentType").val(d.studentType);
                $("#studentProvince").val(d.studentProvince);
                $("#middleSchool").val(d.middleSchool);
                $("#date").val(d.date);
                $("#major").val(d.major);

                //$("#date").attr('placeholder',data.date);

                $.each(d.grades, function (i, grade) {
                    console.log(grade);

                    const mode = 
                    '<div class="row-fluid">'+
                        '<div class="span1">'+
                            '<div>'+
                                '<p id="id"></p>'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="name" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span1">'+
                            '<div>'+
                                '<input class="input-mini disabled" id="credit" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="type" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="semester" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="score" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2" id="grade_active">'+
                        '</div>'+
                    '</div>';
                    
                    const $obj = $(mode);
                    $obj.find("#id").text(grade.id);
                    $obj.find("#name").val(grade.name);
                    $obj.find("#credit").val(grade.credit);
                    $obj.find("#type").val(grade.type);
                    $obj.find("#semester").val(grade.semester);
                    $obj.find("#score").val(grade.score);

                    $("#grade_body").append($obj);

                })
            }

        }, false);
    
        source.addEventListener('error', function (e) {
            console.log("异常：" + e.data);
        });

        const item ={
            "index":$("#index").val(),
            "userId":userId,
            "peer":peer
        }

        $.ajax({
            url:url+"studentInfo/sseSent",
            data:JSON.stringify(item),
            type: "post",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
               
            },
            error:function(id){
               
            }
        })

    }
});

$(document).ready(function () {

    const url = getUrl();

    $.ajax({
        url:url+"studentInfo/getDetail",
        data:{},
        type: "get",
        contentType: "application/json",
        success: function(map) {
            console.log(map);

            const data = map.data;

            if(map=="unLogin"){
                location.href="login.html";
            }else if(map.error=="true"){
                console.log("粗错了")
            }else{
                $('#error').hide();
                $("#page").val(map.maxPage);
                $("#maxPage").val(map.maxPage);
                $("#hash").val(map.hash);
                $("#index").val(map.index);

                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#sex").val(data.sex);
                $("#idNumber").val(data.idNumber);
                $("#birth").val(data.birth);
                $("#nation").val(data.nation);
                $("#_class").val(data._class);
                $("#level").val(data.level);
                $("#college").val(data.college);
                $("#place").val(data.place);
                $("#postCode").val(data.postCode);
                $("#status").val(data.status);
                $("#phone").val(data.phone);
                $("#mail").val(data.mail);
                $("#studentType").val(data.studentType);
                $("#studentProvince").val(data.studentProvince);
                $("#middleSchool").val(data.middleSchool);
                $("#date").val(data.date);
                $("#major").val(data.major);

                //$("#date").attr('placeholder',data.date);

                $.each(data.grades, function (i, grade) {
                    console.log(grade);

                    const mode = 
                    '<div class="row-fluid">'+
                        '<div class="span1">'+
                            '<div>'+
                                '<p id="id"></p>'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="name" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span1">'+
                            '<div>'+
                                '<input class="input-mini disabled" id="credit" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="type" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="semester" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="score" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2" id="grade_active">'+
                        '</div>'+
                    '</div>';
                    
                    const $obj = $(mode);
                    $obj.find("#id").text(grade.id);
                    $obj.find("#name").val(grade.name);
                    $obj.find("#credit").val(grade.credit);
                    $obj.find("#type").val(grade.type);
                    $obj.find("#semester").val(grade.semester);
                    $obj.find("#score").val(grade.score);

                    $("#grade_body").append($obj);

                })

                $.each(map.ws, function (i, wsAdr) {
                    console.log(wsAdr);

                    const option = '<option>'+wsAdr+'</option>';

                    const $obj2 = $(option);

                    $("#peerSelect").append($obj2);
                })

            }

            
            
        },
        error:function(){
            console.log("----------")
        }
    })

    showPage();
    
});

function showPage(){
    $("#pageActive").empty();

    const page1 =
    '<div class="span2">'+
		'<h3 id="prePage">< 上一记录</h3>'+
	'</div>'+
    '<div class="span9"></div>'+
    '<div class="span1">'+
		'<h3 id="nextPage">下一记录 ></h3>'+
	'</div>';

    const page2 =
    '<div class="span2">'+
	'</div>'+
    '<div class="span9"></div>'+
    '<div class="span1">'+
		'<h3 id="nextPage">下一记录 ></h3>'+
	'</div>';

    const page3 =
    '<div class="span2">'+
		'<h3 id="prePage">< 上一记录</h3>'+
	'</div>'+
    '<div class="span9"></div>'+
    '<div class="span1">'+
	'</div>';

    const page4 =
    '<div class="span2">'+
	'</div>'+
    '<div class="span9"></div>'+
    '<div class="span1">'+
	'</div>';

    const $p1 = $(page1);
    const $p2 = $(page2);
    const $p3 = $(page3);
    const $p4 = $(page4);

    var pagenum =$("#page").val();
    var pageMax =$("#maxPage").val();
    console.log(pagenum);
    console.log(pageMax);
    if(pageMax==1){
        $("#pageActive").append($p4);
    }else if(pagenum==pageMax){
        $("#pageActive").append($p3);
    }else if(pagenum==1){
        $("#pageActive").append($p2);
    }else{
        $("#pageActive").append($p1);
    }
}

$("#pageActive").on('click','#prePage',function(){
    var pagenum =Number($("#page").val())-1;
    $("#page").val(pagenum);
    showPage();
    
    const url = getUrl();

    $.ajax({
        url:url+"studentInfo/getDataByPage",
        data:{
            page:pagenum
        },
        type: "get",
        contentType: "application/json",
        success: function(map) {
            console.log(map);

            const data = map.data;

            if(map=="unLogin"){
                location.href="login.html";
            }else{

                
                $("#maxPage").val(map.maxPage);
                $("#index").val(map.index);

                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#sex").val(data.sex);
                $("#idNumber").val(data.idNumber);
                $("#birth").val(data.birth);
                $("#nation").val(data.nation);
                $("#_class").val(data._class);
                $("#level").val(data.level);
                $("#college").val(data.college);
                $("#place").val(data.place);
                $("#postCode").val(data.postCode);
                $("#status").val(data.status);
                $("#phone").val(data.phone);
                $("#mail").val(data.mail);
                $("#studentType").val(data.studentType);
                $("#studentProvince").val(data.studentProvince);
                $("#middleSchool").val(data.middleSchool);
                $("#date").val(data.date);

                //$("#date").attr('placeholder',data.date);

                $("#grade_body").empty();

                $.each(data.grades, function (i, grade) {
                    console.log(grade);

                    const mode = 
                    '<div class="row-fluid">'+
                        '<div class="span1">'+
                            '<div>'+
                                '<p id="id"></p>'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="name" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span1">'+
                            '<div>'+
                                '<input class="input-mini disabled" id="credit" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="type" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="semester" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="score" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2" id="grade_active">'+
                        '</div>'+
                    '</div>';
                    
                    const $obj = $(mode);
                    $obj.find("#id").text(grade.id);
                    $obj.find("#name").val(grade.name);
                    $obj.find("#credit").val(grade.credit);
                    $obj.find("#type").val(grade.type);
                    $obj.find("#semester").val(grade.semester);
                    $obj.find("#score").val(grade.score);

                    $("#grade_body").append($obj);

                })

            }

            
            
        },
        error:function(){
            console.log("----------")
        }
    })
});


$("#pageActive").on('click','#nextPage',function(){
    var pagenum =Number($("#page").val())+1;
    $("#page").val(pagenum);
    showPage();

    const url = getUrl();

    $.ajax({
        url:url+"studentInfo/getDataByPage",
        data:{
            page:pagenum
        },
        type: "get",
        contentType: "application/json",
        success: function(map) {
            console.log(map);

            const data = map.data;

            if(map=="unLogin"){
                location.href="login.html";
            }else{

                $("#maxPage").val(map.maxPage);
                $("#index").val(map.index);

                $("#number").val(data.number);
                $("#name").val(data.name);
                $("#sex").val(data.sex);
                $("#idNumber").val(data.idNumber);
                $("#birth").val(data.birth);
                $("#nation").val(data.nation);
                $("#_class").val(data._class);
                $("#level").val(data.level);
                $("#college").val(data.college);
                $("#place").val(data.place);
                $("#postCode").val(data.postCode);
                $("#status").val(data.status);
                $("#phone").val(data.phone);
                $("#mail").val(data.mail);
                $("#studentType").val(data.studentType);
                $("#studentProvince").val(data.studentProvince);
                $("#middleSchool").val(data.middleSchool);
                $("#date").val(data.date);

                //$("#date").attr('placeholder',data.date);

                $("#grade_body").empty();

                $.each(data.grades, function (i, grade) {
                    console.log(grade);

                    const mode = 
                    '<div class="row-fluid">'+
                        '<div class="span1">'+
                            '<div>'+
                                '<p id="id"></p>'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="name" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span1">'+
                            '<div>'+
                                '<input class="input-mini disabled" id="credit" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="type" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="semester" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2">'+
                            '<div>'+
                                '<input class="input-medium disabled" id="score" type="text" placeholder="" disabled="">'+
                            '</div>'+
                        '</div>'+
                        '<div class="span2" id="grade_active">'+
                        '</div>'+
                    '</div>';
                    
                    const $obj = $(mode);
                    $obj.find("#id").text(grade.id);
                    $obj.find("#name").val(grade.name);
                    $obj.find("#credit").val(grade.credit);
                    $obj.find("#type").val(grade.type);
                    $obj.find("#semester").val(grade.semester);
                    $obj.find("#score").val(grade.score);

                    $("#grade_body").append($obj);

                })

            }

            
            
        },
        error:function(){
            console.log("----------")
        }
    })
});